package cn.webrx.bs.service;

import cn.webrx.bs.entity.Book;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-08-31 17:52
 */
public class BookDaoImpl implements BookDao {


    @Override
    public long count() {
        long num = 0;
        try{
            num = run.query("select count(*) from bs_book",new ScalarHandler<>());
        }catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public long count(String w) {
        long num = 0;
        try{
            num = run.query("select count(*) from bs_book where " + w,new ScalarHandler<>());
        }catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public List<Book> searchByName(String name,int currpage,int pagesize) {
        List<Book> list = null;
        try {
            list = run.query("select * from bs_book where online and name like ? limit ?,?",new BeanListHandler<>(Book.class),"%"+name+"%",currpage*pagesize-pagesize,pagesize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Book> queryAllByCateId(int id) {
        List<Book> list = null;
        try {
            list = run.query("SELECT * FROM bs_book WHERE cateid=? OR cateid IN (SELECT id FROM bs_bookcates WHERE cpath LIKE ?)", new BeanListHandler<>(Book.class), id, "\'%," + id + ",%\'");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Book> queryByParentId(int id) {
        List<Book> list = null;
        try {
            list = run.query("select id,name,imgs->>'$[0]' imges,price,num from bs_book where online and cateid=? order by id desc limit 8", new BeanListHandler<>(Book.class), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Book queryById(int id) {
        Book book = null;
        try {
            book = run.query("select * from bs_book where id = ?", new BeanHandler<>(Book.class), id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return book;
    }

    @Override
    public int add(Book book) {
        int num = 0;
        try {
            num = run.update("insert into bs_book(name,imgs,author,price,nprice,num,online,cateid) values(?,?,?,?,?,?,?,?)", book.getName(), book.getImges(), book.getAuthor(), book.getPrice(), book.getNprice(), book.getNum(), book.isOnline(), book.getCateid());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public List<Book> queryAll() {
        List<Book> list = null;
        try {
            list = run.query("select * from bs_book order by id desc", new BeanListHandler<>(Book.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Book> show() {
        List<Book> list = null;
        try {
            list = run.query("select id,name,imgs->>'$[0]' imges,price,num  from bs_book where online order by id desc", new BeanListHandler<>(Book.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
